import { Entity, Column, PrimaryGeneratedColumn, ManyToMany, JoinTable } from 'typeorm'
import { UserEntity } from 'src/user/entities/user.entity'
import { MenuEntity } from 'src/menu/entities/menu.entity'
import { ApiProperty } from '@nestjs/swagger'


@Entity('role')
export class RoleEntity {
  @PrimaryGeneratedColumn()
  id: string

  @ApiProperty({ description: '角色名称' })
  @Column()
  name: string

  @ManyToMany(() => UserEntity, (user) => user.roles)
  users: UserEntity[]

  @ManyToMany(() => MenuEntity, (menu) => menu.roles)
  @JoinTable({
    name: 'role_menus',
    joinColumns: [
      { name: 'roleId', referencedColumnName: 'id' }
    ],
    inverseJoinColumns: [
      { name: 'menuId', referencedColumnName: 'id' }
    ]
  })
  menus: MenuEntity[]

  @Column({ type: 'timestamp', name: `create_time`, default: () => 'CURRENT_TIMESTAMP' })
  createTime: Date

  @Column({ type: 'timestamp', name: `update_time`, default: () => 'CURRENT_TIMESTAMP' })
  updateTime: Date
}
